package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Pood;
import cn.stylefeng.guns.modular.api.mapper.PoodMapper;
import cn.stylefeng.guns.modular.api.model.params.PoodParam;
import cn.stylefeng.guns.modular.api.model.result.PoodResult;
import cn.stylefeng.guns.modular.api.service.PoodService;
import cn.stylefeng.guns.modular.api.service.WarehouseService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 养殖池 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class PoodServiceImpl extends ServiceImpl<PoodMapper, Pood> implements PoodService {


    @Autowired
    private WarehouseService warehouseService;

    @Override
    public void add(PoodParam param) {
        try {
            Pood entity = getEntity(param);
            this.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void deleteParam(PoodParam param) {
        this.baseMapper.remove(param);
    }
    @Override
    public void delete(PoodParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(PoodParam param) {
        Pood oldEntity = getOldEntity(param);
        Pood newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PoodResult findBySpec(PoodParam param) {
        return null;
    }

    @Override
    public List<PoodResult> findListBySpec(PoodParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PoodParam param) {
        Page pageContext = getPageContext();
        if (pageContext.getSize() == 0) {
            List datas = this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(PoodParam param) {
        return param.getPoodId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Pood getOldEntity(PoodParam param) {
        return this.getById(getKey(param));
    }

    private Pood getEntity(PoodParam param) {
        Pood entity = new Pood();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
